package com.xiaomei.manager.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaomei.manager.model.DiscountAddress;
import com.xiaomei.manager.model.DiscountAddress;
import com.xiaomei.manager.model.DiscountAddress;
import com.xiaomei.manager.service.IDiscountAddressService;
import com.xiaomei.manager.utils.Constants;
import com.xiaomei.manager.web.aliyun.OSS.AliyunUpload;
import com.xiaomei.manager.web.dto.req.AddressQueryDto;
import com.xiaomei.manager.web.page.Page;

@Controller
@RequestMapping("/address")

public class DiscountAddressController {
	@Autowired
	private IDiscountAddressService iDiscountAddressService;
	
	@RequiresPermissions("address:view")
	@RequestMapping()
	public String list(Model model,AddressQueryDto dto,Page<DiscountAddress> page){
		iDiscountAddressService.getDiscountAddressList(dto, page);
		model.addAttribute("dto",dto);
		model.addAttribute("page",page);
	return "address/list";	
	}
	
	/**
     * 优惠地址添加
     * @param model
     * @return
     */
	 @RequiresPermissions("address:create")
	    @RequestMapping( value = "/create",method = RequestMethod.GET)
	    public String showCreateForm(Model model) {	        
	        model.addAttribute("exp", new DiscountAddress());
	        model.addAttribute("op", "新增");
	        return "address/edit";
	    } 
	 @RequiresPermissions("address:create")
	    @RequestMapping(value = "/create",method = RequestMethod.POST)
	    public String create(DiscountAddress exp , RedirectAttributes redirectAttributes) {    	 
	    	iDiscountAddressService.createDiscountAddress(exp);
	        redirectAttributes.addFlashAttribute("msg", "新增成功");
	        return "redirect:/address";
	    }
	 
	 /**
	     * 更新优惠地址内容
	     * @param id
	     * @param model
	     * @return
	     */
	    @RequiresPermissions("address:update")
	    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	    public String showUpdateForm(@PathVariable("id") Integer id, Model model) {
	        model.addAttribute("exp", iDiscountAddressService.findOne(id));
	        model.addAttribute("op", "修改");
	        return "address/edit";
	    }

	    @RequiresPermissions("address:update")
	    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	    public String update(DiscountAddress exp,  @PathVariable("id") Integer id, RedirectAttributes redirectAttributes, HttpServletRequest request){	    		    
	        iDiscountAddressService.updateDiscountAddress(exp);
	        redirectAttributes.addFlashAttribute("msg", "修改成功");
	        return "redirect:/address";
	    }
	    /**
	     * 删除优惠地址
	     * @param id
	     * @param model
	     * @return
	     */
	    @RequiresPermissions("address:delete")
	    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	    public String showDeleteForm(@PathVariable("id") Integer id, Model model) {	       
	    	model.addAttribute("exp", iDiscountAddressService.findOne(id));
	        model.addAttribute("op", "删除");
	        return "address/edit";
	    }

	    @RequiresPermissions("address:delete")
	    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	    public String delete(@PathVariable("id") Integer id, RedirectAttributes redirectAttributes) {	       
	        iDiscountAddressService.deleteDiscountAddress(id);
	        redirectAttributes.addFlashAttribute("msg", "删除成功");
	        return "redirect:/address";
	    }
}
